/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package personal.xml;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import personal.dao.BookDAO;
import personal.jaxb.Books;
import personal.obj.Book;
import personal.obj.Order;
import personal.orders.Orders;

/**
 *
 * @author PHATNBSE61057
 */
public class JAXBMarshall {

    public void marshallAllBookPDF() {
        personal.pdf.Books books = new personal.pdf.Books();
        BookDAO bDAO = new BookDAO();
        List<Book> listBook = new ArrayList<Book>();
        listBook = bDAO.getAllBook();
        for (Book book : listBook) {
            personal.pdf.Book jBook = new personal.pdf.Book();
            jBook.setBookID(book.getBookID());
            jBook.setBookAuthor(book.getBookAuthor());
            jBook.setBookDescription(book.getBookDescription());
            jBook.setBookDetailedEdition(book.getBookDetailedEdition());
            jBook.setBookFormat(book.getBookFormat());
            jBook.setBookISBN(book.getBookISBN());
            jBook.setBookImg(book.getBookImg());
            jBook.setBookLink(book.getBookLink());
            jBook.setBookName(book.getBookName());
            jBook.setBookPages(book.getBookPages());
            jBook.setBookPrice(book.getBookPrice());
            jBook.setBookPublishedDate(book.getBookPublishedDate());
            jBook.setBookThumb(book.getBookThumb());
            jBook.setCategoryID(book.getCategoryID());
            books.getBook().add(jBook);
        }
        try {
            JAXBContext ctx = JAXBContext.newInstance(personal.pdf.Books.class);
            Marshaller mar = ctx.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//            mar.marshal(books, new File("./web/xml/books.xml"));
            mar.marshal(books, new File("C:\\Users\\TruongHoangLe\\Desktop\\XML Project\\XML_Project\\web\\WEB-INF\\books.xml"));
//            mar.marshal(books, new File("C:\\Users\\TruongHoangLe\\Desktop\\XML Project\\XML_Project\\web\\xml\\booksAll.xml"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void marshallAllBook() {
        Books books = new Books();
        BookDAO bDAO = new BookDAO();
        List<Book> listBook = new ArrayList<Book>();
        listBook = bDAO.getAllBook();
        for (Book book : listBook) {
            personal.jaxb.Book jBook = new personal.jaxb.Book();
            jBook.setBookID(book.getBookID());
            jBook.setBookAuthor(book.getBookAuthor());
            jBook.setBookDescription(book.getBookDescription());
            jBook.setBookDetailedEdition(book.getBookDetailedEdition());
            jBook.setBookFormat(book.getBookFormat());
            jBook.setBookISBN(book.getBookISBN());
            jBook.setBookImg(book.getBookImg());
            jBook.setBookLink(book.getBookLink());
            jBook.setBookName(book.getBookName());
            jBook.setBookPages(book.getBookPages());
            jBook.setBookPrice(book.getBookPrice());
            jBook.setBookPublishedDate(book.getBookPublishedDate());
            jBook.setBookThumb(book.getBookThumb());
            jBook.setCategoryID(book.getCategoryID());
            books.getBook().add(jBook);
        }
        try {
            JAXBContext ctx = JAXBContext.newInstance(Books.class);
            Marshaller mar = ctx.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//            mar.marshal(books, new File("./web/xml/books.xml"));
//            mar.marshal(books, new File("C:\\Users\\TruongHoangLe\\Desktop\\XML Project\\XML_Project\\web\\WEB-INF\\books.xml"));
            mar.marshal(books, new File("C:\\Users\\TruongHoangLe\\Desktop\\XML Project\\XML_Project\\web\\xml\\booksAll.xml"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void marshallCategory(Books listBook) {
        try {
            JAXBContext ctx = JAXBContext.newInstance(Books.class);
            Marshaller mar = ctx.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(listBook, new File("C:\\Users\\TruongHoangLe\\Desktop\\XML Project\\XML_Project\\web\\xml\\searchResult.xml"));
//            mar.marshal(listBook, new File("C:\\Users\\TruongHoangLe\\Desktop\\XML Project\\XML_Project\\web\\xml\\searchResult.xml"));

//            mar.marshal(listBook, new File("web/xml/searchResult.xml"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void marshallAddingBook(personal.adding.Book book) {
        try {
            JAXBContext ctx = JAXBContext.newInstance(personal.adding.Book.class);
            Marshaller mar = ctx.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(book, new File("C:\\Users\\TruongHoangLe\\Desktop\\XML Project\\XML_Project\\web\\xml\\addingBook.xml"));

//            mar.marshal(listBook, new File("web/xml/searchResult.xml"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void marshallOrder(List<Order> orderList) {
        Orders orders = new Orders();
        personal.orders.Order order = new personal.orders.Order();
        for (Order o : orderList) {
            order = new personal.orders.Order();
            order.setOrderID(String.valueOf(o.getOrderID()));
            order.setBookingTime(o.getBookingTime().toString());
            order.setUsername(o.getUsername());
            order.setStatusID(String.valueOf(o.getStatusID()));
            order.setTotalPrice(String.valueOf(o.getTotalPrice()));
            orders.getOrder().add(order);
        }
        try {
            JAXBContext ctx = JAXBContext.newInstance(Orders.class);
            Marshaller mar = ctx.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//            mar.marshal(books, new File("./web/xml/books.xml"));
            mar.marshal(orders, new File("C:\\Users\\TruongHoangLe\\Desktop\\XML Project\\XML_Project\\web\\xml\\ordersData.xml"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
